package com.daka.service.impl;

import com.daka.dao.ActivityDao;
import com.daka.entity.Activity;
import com.daka.entity.Member;
import com.daka.entity.Organization;
import com.daka.enumerate.StatusCode;
import com.daka.service.ActivityService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import com.daka.tools.Page;
import com.daka.tools.ServiceRuntimeException;
import com.daka.vo.response.ActivityResponseVo;
import com.daka.vo.response.OrganizationResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 16:39
 */

@Service("activityService")
@Transactional
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private MemberService memberService;

    @Override
    public int insert(Activity po) {
        return activityDao.insert(po);
    }

    @Override
    public int update(Activity po) {
        return activityDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public Activity selectByPrimaryKey(Integer id) {
        return activityDao.selectByPrimaryKey(id);
    }

    @Override
    public Activity selectByActCode(String activityCode) {
        return activityDao.selectByActCode(activityCode);
    }

    @Override
    public List<Activity> selectByStatement(Activity po) {
        return activityDao.selectByStatement(po);
    }

    @Override
    public Page<ActivityResponseVo> selectByGroup(String groupCode, String pageNo) {
        int pageNo1;
        if(pageNo==null || Integer.valueOf(pageNo)<1){
            pageNo1 =1;
        }else{
            pageNo1=Integer.valueOf(pageNo);
        }
        Organization organization = new Organization();
        organization.setGroupCode(groupCode);
        List<Organization> listOrg = organizationService.selectByStatement(organization);
        if(listOrg==null || listOrg.size()<1){
            throw new ServiceRuntimeException(StatusCode.ACTIVITY_NOT_EXISTS.getCode(),StatusCode.ACTIVITY_NOT_EXISTS.getMessage());
        }
        List<Activity> list = activityDao.selectByGroup(listOrg.get(0).getId(), 10, 10 * (pageNo1 - 1));

        List<ActivityResponseVo> listVos = new ArrayList<ActivityResponseVo>();
        for(Activity aa:list){
            ActivityResponseVo vo = ActivityResponseVo.buildVO(aa);
            listVos.add(vo);
        }
        int count = activityDao.selectByGroupCount(listOrg.get(0).getId());
        Page<ActivityResponseVo> page =  new Page<ActivityResponseVo>();
        page.setList(listVos);
        page.setTotal(count);
        return page;
    }


    @Override
    public Page<ActivityResponseVo> selectByMember(String memberID, String pageNo) {
        int pageNo1;
        if(pageNo==null || Integer.valueOf(pageNo)<1){
            pageNo1 =1;
        }else{
            pageNo1=Integer.valueOf(pageNo);
        }
        Member member = new Member();
        member.setMemberID(memberID);
        List<Member> listMe = memberService.selectByStatement(member);
        if(listMe==null || listMe.size()<1){
            throw new ServiceRuntimeException(StatusCode.VOLUNTEER_NOT_EXISTS.getCode(),StatusCode.VOLUNTEER_NOT_EXISTS.getMessage());
        }
        List<Activity> list = activityDao.selectByMember(listMe.get(0).getId(), 10, 10 * (pageNo1 - 1));
        List<ActivityResponseVo> listVos = new ArrayList<ActivityResponseVo>();
        for(Activity aa:list){
            ActivityResponseVo vo = ActivityResponseVo.buildVO(aa);
            listVos.add(vo);
        }
        int count = activityDao.selectByMemberCount(listMe.get(0).getId());
        Page<ActivityResponseVo> page =  new Page<ActivityResponseVo>();
        page.setList(listVos);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<Activity> selectApplyByUser(Integer member) {
        return activityDao.selectApplyByUser(member);
    }

}



